package com.redpacketcover.rc.base;

import android.content.Intent;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.redpacketcover.rc.common.ToastHelp;

public abstract class ModelObserver<T> implements Observer<BaseResponseBean<T>> {

    public abstract void onSuccess(T data);

    public abstract void onFailed(int code, String errMsg);

    /**
     * 剥离需要的数据返回给上层
     * 接口请求成功 并不代表真正的成功
     * 需要对业务的code 判断  再分情况返回给上层
     */
    @Override
    public void onChanged(@Nullable BaseResponseBean<T> baseResponseBean) {
        int statusCode = baseResponseBean.getCode();
        if (statusCode == 200) {
            onSuccess(baseResponseBean.getData());
        } else if (statusCode == 401) {
            ToastHelp.toast("登录失效，请重新登录");
//            Intent intent = new Intent();
//            intent.setAction("android.intent.action.rc");
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            BaseApplication.getInstance().startActivity(intent);
        } else {
            onFailed(statusCode, baseResponseBean.getMsg());
        }
    }

}
